/*
 * Created on Jun 23, 2004
 */
package org.vectrics.recruiter.job;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.vectrics.SystemException;


/**
 * This class will instantiate the scheduler for running job candidate skill matching.
 */
public class JobCandidateSkillMatchScheduler {
	private transient final Log log = LogFactory.getLog(JobCandidateSkillMatchScheduler.class);
	
	/**
	 * Constructor
	 */
	public JobCandidateSkillMatchScheduler() {
		super();
	}

	/**
	 * Starts the scheduler for finding job agents needing update.
	 */
	public void start() {
		try {	
			log.debug("Schedule Job Candidate SKill Matching");
			
			String triggerName = "Job Candidate Skill Trigger";
			String groupName = "flashrecruit";
			String jobName = this.getClass().getName();
			
			
			SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
			Scheduler sched = schedFact.getScheduler();
			sched.deleteJob(jobName, groupName);
			
			Trigger trigger = sched.getTrigger(triggerName, groupName);   
			//"0 0/5 14 * * ?"    Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day 		
			//trigger = new CronTrigger(triggerName, group, "0 0/5 14 * * ?");
			trigger = new SimpleTrigger("myTrigger",
                    Scheduler.DEFAULT_GROUP,
                    new Date(),
                    null,
                    SimpleTrigger.REPEAT_INDEFINITELY,
                    2 * 60L * 1000L 			// 30 minutes
                    //1L * 60L * 60L * 1000L 			// 1 Hours
                    );
			
			JobDetail jobDetail = sched.getJobDetail(this.getClass().getName(),
                    Scheduler.DEFAULT_GROUP);
			
			
			sched.start();
			if (jobDetail == null) {
			    jobDetail = new JobDetail(jobName,
	                                      groupName,
                                     InvokeJobCandidateSkillMatchQuartzJob.class);
	 			sched.scheduleJob(jobDetail, trigger);
			}
			
			jobDetail.setDescription("Job Agent Processing");
			jobDetail.setGroup(groupName);
			
			Date date = trigger.getFireTimeAfter(new Date());
			log.info("Agent job started, next fire time: " + date);
	
		} catch (Exception ex) {
			log.error("Exception starting job agent schedule", ex);
			throw new SystemException("Exception starting job agent schedule", ex);
		}
	}
	
}
